//
//  QrCodeViewController.m
//  JxCarios
//
//  Created by phev on 15/3/4.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "QrCodeViewController.h"
#import "QREncoder.h"
#import "CarAroundDataSource.h"
#import "CurrentDataSource.h"
#import "ARefundViewController.h"
#define QRGENERATEKEY @"ORDER_UUID"

@interface QrCodeViewController ()<UIAlertViewDelegate>
{
    CarAroundDataSource *carDataSource;
    CurrentDataSource *dataSource;;
}
@end

@implementation QrCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initAbout];
    [self setUp];
    [self netRequst];
    
}

- (void)viewWillAppear:(BOOL)animated {
    if (carDataSource) {
        [self netRequst];
        
    }
}

- (void)netRequst {
    if (!carDataSource) {
        carDataSource = [CarAroundDataSource dataSourceWithRequestor:self];
    }
//    0--未支付
//    1--已支付
//    2--已服务完成
//    3--已完成(暂不要)
//    4--取消
//    5--退款
//    6--申诉
    carDataSource.shopId = [_info customObjectForkey:@"ID"];
    [carDataSource getOrderTableDetail:^(id result) {
        NSString *tempType = nil;
        NSDictionary *dic = [result customObjectForkey:@"detail"];
        _dianpuAddress.text = [dic customObjectForkey:@"SHOP_ADDR"];
        _xiadanDate.text = [[[dic customObjectForkey:@"CREATE_DATE"] componentsSeparatedByString:@" "] firstObject];
        tempType =  [dic customObjectForkey:@"ORDER_STATE"];
        if ([tempType isEqualToString:@"0"]) {
            _payType.text = @"未支付";
            _sqTkView.hidden = YES;
        }else if ([tempType isEqualToString:@"1"]) {
            _payType.text = @"已支付";
        }else if ([tempType isEqualToString:@"2"]) {
            _payType.text = @"已服务";
        }else if ([tempType isEqualToString:@"3"]) {
            _payType.text = @"已完成";
        }else if ([tempType isEqualToString:@"4"]) {
            _payType.text = @"取消";
        }else if ([tempType isEqualToString:@"5"]) {
            _payType.text = @"退款";
        }else {
            _payType.text = @"申诉";
        }
        [_phone setTitle:[dic customObjectForkey:@"TEL"] forState:UIControlStateNormal];
        [_phone addTarget:self action:@selector(phoneAction:) forControlEvents:UIControlEventTouchUpInside];
        _canpingName.text = [dic customObjectForkey:@"GOOD_NAME"];
        _orderType = [[dic customObjectForkey:@"ORDER_STATE"] intValue];
        _price.text = [dic customObjectForkey:@"ORDER_PRICE"];  
    } anfFail:^(id result) {
        
    }];
    
}

- (void)initAbout {
    self.title = [_barTitle stringByAppendingString:@"订单"];
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
}

- (void)setUp {
    if (_info.allKeys.count == 0 ) {
        return;
    }
    if(_type == lineValue){
        //TODO:退款按钮
        _sqTkView.hidden = NO;
    }
    
    //生成二维码
    _erweima.image = [QREncoder encode:[_info customObjectForkey:@"ORDER_ID"]];
   // _erweima.image = [QREncoder encode:@"www.baidu.com"];
    _dingdanming.text = [[@"确认[" stringByAppendingString:[_info customObjectForkey:@"GOOD_NAME"]] stringByAppendingString:@"]订单"];
    _payType.text = _barTitle;
    _orderId.text = [_info customObjectForkey:@"ORDER_ID"];
 }

//申请退款
- (IBAction)sqtuikuan:(id)sender {
    if([_payType.text isEqualToString:@"退款"]) {
        [Common showAlert:@"提示" message:@"订单处于退款状态,请勿重复退款"];
        return;
    }
    ARefundViewController *arefund = [[ARefundViewController alloc] initWithNibName:@"ARefundViewController" bundle:nil];
    arefund.info = _info;
    [self.navigationController pushViewController:arefund animated:YES];
}

- (void)phoneAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    [Common callPhone:btn.titleLabel.text andControl:self.view];
}

- (IBAction)commentsAction:(UIButton *)sender {
    if (_orderType != 1 && _orderType != 2 && _orderType != 3) {
        [self.view makeToast:@"订单未完成,无法评价"];
        return;
    }
    if(!dataSource){
        dataSource = [CurrentDataSource dataSourceWithRequestor:self];
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"服务评价"
                                                    message:@"商家已完成订单服务\n可以对该服务进行评价"
                                                   delegate:self
                                          cancelButtonTitle:@"赞一个"
                                          otherButtonTitles:@"还需努力", nil];
    alert.tag = 202;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *goodOrNo;
    if (buttonIndex == 0) {
         goodOrNo = @"1";
    }else {
        goodOrNo = @"0";
    }
    dataSource.orderId = _orderId.text;
    dataSource.goodOrno = goodOrNo;
    [dataSource clickAgree:^(id result) {
        NSLog(@"%@",result);
    } andFail:^(id result) {
        NSLog(@"%@",result);

    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
@end
